/**
 * @author P012410
 */
var rollingtimer = null;  		//롤링 타이머
var $top_promtion;				//프로모션 콘테이너
var $mainimg;					//메인이미지
var $list_section;				//롤링이미지 리스트 영역
var currentthumbindex= 0;		//현재 선택 썸네일 인덱스		
var rollinginterval = 2000;   	//자동 롤링 간격(단위 : ms)
var thumbwidth;					//썸네일 너비
var licnt;						//썸네일 갯수
var pagesize = 4;				//화면당 썸네일 갯수
var $thumbimgs = null;
var $list_section = null;
var $ulpromotion =  null;
		
//프로모션 롤링이미지 롤링 시작
function StartRollingBanner()
{
	rollingtimer = window.setInterval('RollingBanner()', rollinginterval);
}

//프로모션 자동 이미지 롤링	
function RollingBanner()
{
	currentthumbindex++;
	
	if(licnt<= pagesize && currentthumbindex == licnt)
	{		
		currentthumbindex = 0;			
	}
	
	if(licnt > pagesize && currentthumbindex == pagesize)
	{			
		currentthumbindex = 0;		
		
		var $prevlist = $('ul>li:lt(' +pagesize +')', $top_promtion);
		var $tprevlist = $prevlist.clone(true);
		$('ul', $top_promtion).append($tprevlist);
		$prevlist
			.animate(
				{marginLeft : (thumbwidth * pagesize)* -1}, 
				'fast',  
				function(){
					$prevlist.remove();
					$thumbimgs = $('ul>li>img', $('div.top_promtion'));	
					$thumbimgs.unbind('click').unbind('mouseenter').unbind('mouseleave');				
					BindThumbEvent();
					SwapMainImgInfo(currentthumbindex);					
											
		});				
	}
	
	SwapMainImgInfo(currentthumbindex);		
}	

//프로모션 이미지 롤링 멈추기
function PauseRollingBanner()
{
	if (rollingtimer) {
		window.clearInterval(rollingtimer);		
	}
}	

//메인 이미지 교체
function SwapMainImgInfo(senderidx){	
	var $thumbimgs = $('ul>li>img', $top_promtion);
	var $sender =$thumbimgs.eq(senderidx);
	var $firstHref = $sender.next('a:first');
	var target ='';
				
	bigImg = $sender.attr('bigImg');
	
	$mainimg.attr('src', bigImg);					
	target =  $firstHref.attr('target');
	if(target=='') target = '_self';
		
	$mainimg.parent('a').attr({'href' : $firstHref.attr('href'), 'target' : target});
	
	$thumbimgs.css({filter: "Alpha(opacity=40)", opacity:"0.4"}).attr('selectYn', 'N');
	$sender.css({filter:"", opacity:"1"}).attr('selectYn', 'Y');	
}

function GetSelectedThumbIndex(sender)
{
	var $thumbs = $('ul>li>img', $top_promtion);
	return $thumbs.index(sender);		
}


function tMovePrev()
{	
	if(currentthumbindex == 0) 
	{
		var $ulList = $('ul>li', $top_promtion);
		var lastidx = $ulList.length - 1;		
		var cpyidx = lastidx - pagesize;		
		var $prevlist = $ulList.filter(':gt(' + cpyidx + ')');
		
		$prevlist.css({marginLeft: (thumbwidth * pagesize) * -1})
			.insertBefore($ulList.filter(':eq(0)'))
			.animate(
				{marginLeft: 0}, 
				'fast', function()
				{
					currentthumbindex = 3;
					SwapMainImgInfo(currentthumbindex);					
				});
	}
	else
	{
		currentthumbindex--;
		SwapMainImgInfo(currentthumbindex);
	}																								
}
	
function BindThumbEvent()
{
	$.each($thumbimgs, function(index, element){
				var $this = $(element);
				
				$this.bind({
					click : function(event)
					{
						event.preventDefault();
						var rid = $this.attr('RID');
						if(rid == 'RSA'){
							rid = rid+(index%4+16);
							Common.clickTfficStatAjax({pid:'nhp1',rid:rid});
						} else if(rid == 'RSD'){
							rid = rid+(index%4+13);
							Common.clickTfficStatAjax({pid:'nhp1',rid:rid});
						} else if(rid == 'RSH'){
							rid = rid+(index%4+6);
							Common.clickTfficStatAjax({pid:'nhp1',rid:rid});
						}
					},
					mouseenter : function(event)
					{
						PauseRollingBanner();
						var idx = GetSelectedThumbIndex(event.currentTarget);
						currentthumbindex = idx;
						SwapMainImgInfo(idx);																
					},					
					mouseleave : function(event)
					{							
						StartRollingBanner();
					}
				});
			});	
}

jQuery.fn.promotion = function(){		
	return this.each(function(index, element){		
				
		function InitPomotion(){
			$top_promtion = $(element);
			$mainimg = $('img:first', $top_promtion);
			$list_section = $('div.list_section.clfix', $top_promtion);
			$ulpromotion = $list_section.find('ul');
				
			licnt = $ulpromotion.find('li').length;			
			$thumbimgs = $ulpromotion.find('img');			
			
			thumbwidth= $ulpromotion.find('li').eq(0).find('img').eq(0).width();		
			$ulpromotion.css({'width' : (thumbwidth*licnt) + 'px'});						
		}
			
		
		function BindEvents()
		{
			var $prev = $list_section.find('a:first');
			var $next = $list_section.find('a:last');
				
			$mainimg.bind({
				mouseenter : function(event)
				{
					PauseRollingBanner();
				},
				mouseleave : function(event)
				{
					StartRollingBanner();				
				}			
			});	
			
			//프로모션 썸네일 이미지  이벤트 바인딩					
			BindThumbEvent();
			
			//이전 버튼 이벤트 바인딩
			$prev.bind(
			{
				click: function(e){
					e.preventDefault();
					
					if (licnt <= 4) 
					{
						if(currentthumbindex!=0) currentthumbindex--;
						else currentthumbindex = licnt-1;
						
						SwapMainImgInfo(currentthumbindex);
						
					}
					else 
					{	
						$(this).attr('disabled', '');										
						tMovePrev();
						$(this).removeAttr('disabled');					
					}
										
					this.blur();			
				},
				mouseenter : function(event)
				{
					PauseRollingBanner();
				},
				mouseleave : function(event)
				{
					StartRollingBanner();				
				}	
			});
			
			//이후 버튼 이벤트 바인딩
			$next.bind({
				click : function(e){			
					e.preventDefault();
					
					if (licnt <= 4) 
					{
						if(currentthumbindex!=licnt-1) currentthumbindex++;
						else currentthumbindex = 0;
						
						SwapMainImgInfo(currentthumbindex);
					}
					else 
					{
						$(this).attr('disabled', '');
						RollingBanner();						
						$(this).removeAttr('disabled');		
					}
					
					this.blur();				
				},
				mouseenter : function(event)
				{
					PauseRollingBanner();
				},
				mouseleave : function(event)
				{
					StartRollingBanner();				
				}
			});	
		}
		
		InitPomotion();   	//프로모션 관련 초기화		
		SwapMainImgInfo(0);	//첫번째 프로모션 선택
		BindEvents();		//프로모션 관련 이벤트 바인딩
		StartRollingBanner();
	});
};
